Write function alternateCase which switch every letter in string from upper to lower and from lower to upper. E.g: Hello World -> hELLO wORLD
把字串中的字母換成相反的大小寫字母
(ns kata.alternate-case-test
(require [clojure.test :refer :all])
(use [kata.alternate-case :rename {alternate-case solution}]))
(deftest sample-tests
(is (= (solution "") ""))
(is (= (solution "abc") "ABC"))
(is (= (solution "Hello World!") "hELLO wORLD!"))
(is (= (solution "CodeWars") "cODEwARS"))
(is (= (solution "i LIKE MAKING KATAS VERY MUCH!") "I like making katas very much!"))
(is (= (solution "0 gravity 0 calories!") "0 GRAVITY 0 CALORIES!")))
Each alphabet checked by isLowerCase
or isUpeerCase
and turned to opposite case by toUpperCase
& toLowerCase
. If other conditions happens (eg. space, punctuation marks) will return itself.
(ns kata.alternate-case)
(defn switch-case [a]
(cond
(Character/isLowerCase a) (Character/toUpperCase a)
(Character/isUpperCase a) (Character/toLowerCase a)
:else a)
)
(defn solution [s]
(apply str (map switch-case s))
)